/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.atomojo.app.db;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Map;
import java.util.TreeMap;

/**
 *
 * @author alex
 */
public class DBConnection {

   Connection connection;
   Map<Integer,PreparedStatement> statements;
   
   DBConnection(Connection connection)
   {
      this.connection = connection;
      this.statements = new TreeMap<Integer,PreparedStatement>();
   }
   
   public PreparedStatement getStatement(int id) 
      throws SQLException
   {
      PreparedStatement statement = statements.get(id);
      statement.clearParameters();
      return statement;
   }
   
   public void addStatement(int id,String sql)
      throws SQLException
   {
      PreparedStatement prepared = connection.prepareStatement(sql);
      statements.put(id, prepared);
   }
   
   public void removeStatement(int id) 
      throws SQLException
   {
      PreparedStatement statement = statements.remove(id);
      statement.close();
   }
   
   public Connection getConnection() {
      return connection;
   }
   
   public void close() 
      throws SQLException
   {
      for (PreparedStatement statement : statements.values()) {
         statement.close();
      }
      statements.clear();
   }
   
}
